﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using OgilvyOne.IDAL;
using OgilvyOne.SqlServerDAL;
using OgilvyOne.Model;//Please add references
namespace OgilvyOne.SQLServerDAL
{
    /// <summary>
    /// 数据访问类:ActivityPhotosManageDal
    /// </summary>
    public partial class ActivityPhotosManageDal : IActivityPhotosManage
    {
        string connstr = SQLHelper.CONN_STRING_NON_DTC;
        public ActivityPhotosManageDal()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string PHOTO_TITLE, int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from COFCO_ACTIVITY_PHOTOS");
            strSql.Append(" where PHOTO_TITLE=@PHOTO_TITLE and PHOTO_ID not in (" + id + ")");
            SqlParameter[] parameters = {
					new SqlParameter("@PHOTO_TITLE", SqlDbType.VarChar,200)
};
            parameters[0].Value = PHOTO_TITLE;

            return int.Parse(SQLHelper.ExecuteScalar(connstr, CommandType.Text, strSql.ToString(), parameters).ToString()) > 0;
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ActivityPhotosManageModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into COFCO_ACTIVITY_PHOTOS(");
            strSql.Append("ACTIVITY_ID,PHOTO_TITLE,MEMBER_ID,ADDTIME,STATE,AUDIT_TIME,AUDIT_USERNAME,AUDIT_STATE,PHOTO_IMG_FILE)");
            strSql.Append(" values (");
            strSql.Append("@ACTIVITY_ID,@PHOTO_TITLE,@MEMBER_ID,@ADDTIME,@STATE,@AUDIT_TIME,@AUDIT_USERNAME,@AUDIT_STATE,@PHOTO_IMG_FILE)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@ACTIVITY_ID", SqlDbType.Int,4),
					new SqlParameter("@PHOTO_TITLE", SqlDbType.VarChar,200),
					new SqlParameter("@MEMBER_ID", SqlDbType.Int,4),
					new SqlParameter("@ADDTIME", SqlDbType.DateTime),
					new SqlParameter("@STATE", SqlDbType.Int,4),
					new SqlParameter("@AUDIT_TIME", SqlDbType.DateTime),
					new SqlParameter("@AUDIT_USERNAME", SqlDbType.VarChar,10),
					new SqlParameter("@AUDIT_STATE", SqlDbType.Int,4),
					new SqlParameter("@PHOTO_IMG_FILE", SqlDbType.VarChar,200)};
            parameters[0].Value = model.Activityid;
            parameters[1].Value = model.Phototitle;
            parameters[2].Value = model.Memberid;
            parameters[3].Value = model.Addtime;
            parameters[4].Value = model.State;

            if (model.Audittime == null)
                parameters[5].Value = DBNull.Value;
            else
                parameters[5].Value = model.Audittime;
            if (model.Auditusername == null)
            {
                parameters[6].Value = DBNull.Value;
            }
            else
                parameters[6].Value = model.Auditusername;
            if (model.Auditstate == null)
            {
                parameters[7].Value = DBNull.Value;
            }
            else
                parameters[7].Value = model.Auditstate;

            parameters[8].Value = model.Photoimgfile;

            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ActivityPhotosManageModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update COFCO_ACTIVITY_PHOTOS set ");
            strSql.Append("ACTIVITY_ID=@ACTIVITY_ID,");
            strSql.Append("PHOTO_TITLE=@PHOTO_TITLE,");
            strSql.Append("MEMBER_ID=@MEMBER_ID,");
            strSql.Append("ADDTIME=@ADDTIME,");
            strSql.Append("STATE=@STATE,");
            strSql.Append("AUDIT_TIME=@AUDIT_TIME,");
            strSql.Append("AUDIT_USERNAME=@AUDIT_USERNAME,");
            strSql.Append("AUDIT_STATE=@AUDIT_STATE,");
            strSql.Append("PHOTO_IMG_FILE=@PHOTO_IMG_FILE");
            strSql.Append(" where PHOTO_ID=@PHOTO_ID");
            SqlParameter[] parameters = {
					new SqlParameter("@ACTIVITY_ID", SqlDbType.Int,4),
					new SqlParameter("@PHOTO_TITLE", SqlDbType.VarChar,200),
					new SqlParameter("@MEMBER_ID", SqlDbType.Int,4),
					new SqlParameter("@ADDTIME", SqlDbType.DateTime),
					new SqlParameter("@STATE", SqlDbType.Int,4),
					new SqlParameter("@AUDIT_TIME", SqlDbType.DateTime),
					new SqlParameter("@AUDIT_USERNAME", SqlDbType.VarChar,10),
					new SqlParameter("@AUDIT_STATE", SqlDbType.Int,4),
					new SqlParameter("@PHOTO_IMG_FILE", SqlDbType.VarChar,200),
                    new SqlParameter("@PHOTO_ID", SqlDbType.Int,4)
                                        };
            parameters[0].Value = model.Activityid;
            parameters[1].Value = model.Phototitle;
            parameters[2].Value = model.Memberid;
            parameters[3].Value = model.Addtime;
            parameters[4].Value = model.State;

            if (model.Audittime == null)
                parameters[5].Value = DBNull.Value;
            else
                parameters[5].Value = model.Audittime;
            if (model.Auditusername == null)
            {
                parameters[6].Value = DBNull.Value;
            }
            else
                parameters[6].Value = model.Auditusername;
            if (model.Auditstate == null)
            {
                parameters[7].Value = DBNull.Value;
            }
            else
                parameters[7].Value = model.Auditstate;

            parameters[8].Value = model.Photoimgfile;
            parameters[9].Value = model.Photoid;

            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int PHOTO_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from COFCO_ACTIVITY_PHOTOS ");
            strSql.Append(" where PHOTO_ID=@PHOTO_ID");
            SqlParameter[] parameters = {
					new SqlParameter("@PHOTO_ID", SqlDbType.Int,4)
};
            parameters[0].Value = PHOTO_ID;

            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters) > 0;
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string PHOTO_IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from COFCO_ACTIVITY_PHOTOS ");
            strSql.Append(" where PHOTO_ID in (" + PHOTO_IDlist + ")  ");
            int rows = SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), null);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ActivityPhotosManageModel GetModel(int PHOTO_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 PHOTO_ID,ACTIVITY_ID,PHOTO_TITLE,MEMBER_ID,ADDTIME,STATE,AUDIT_TIME,AUDIT_USERNAME,AUDIT_STATE,PHOTO_IMG_FILE from COFCO_ACTIVITY_PHOTOS ");
            strSql.Append(" where PHOTO_ID=" + PHOTO_ID);
            ActivityPhotosManageModel model = new ActivityPhotosManageModel();
            DataSet ds = SQLHelper.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["PHOTO_ID"].ToString() != "")
                {
                    model.Photoid = int.Parse(ds.Tables[0].Rows[0]["PHOTO_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ACTIVITY_ID"].ToString() != "")
                {
                    model.Activityid = int.Parse(ds.Tables[0].Rows[0]["ACTIVITY_ID"].ToString());
                }
                model.Phototitle = ds.Tables[0].Rows[0]["PHOTO_TITLE"].ToString();
                if (ds.Tables[0].Rows[0]["MEMBER_ID"].ToString() != "")
                {
                    model.Memberid = int.Parse(ds.Tables[0].Rows[0]["MEMBER_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ADDTIME"].ToString() != "")
                {
                    model.Addtime = DateTime.Parse(ds.Tables[0].Rows[0]["ADDTIME"].ToString());
                }
                if (ds.Tables[0].Rows[0]["STATE"].ToString() != "")
                {
                    model.State = int.Parse(ds.Tables[0].Rows[0]["STATE"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AUDIT_TIME"].ToString() != "")
                {
                    model.Audittime = DateTime.Parse(ds.Tables[0].Rows[0]["AUDIT_TIME"].ToString());
                }
                model.Auditusername = ds.Tables[0].Rows[0]["AUDIT_USERNAME"].ToString();
                if (ds.Tables[0].Rows[0]["AUDIT_STATE"].ToString() != "")
                {
                    model.Auditstate = int.Parse(ds.Tables[0].Rows[0]["AUDIT_STATE"].ToString());
                }
                model.Photoimgfile = ds.Tables[0].Rows[0]["PHOTO_IMG_FILE"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select PHOTO_ID,ACTIVITY_ID,PHOTO_TITLE,MEMBER_ID,ADDTIME,STATE,AUDIT_TIME,AUDIT_USERNAME,AUDIT_STATE,PHOTO_IMG_FILE ");
            strSql.Append(" FROM COFCO_ACTIVITY_PHOTOS ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SQLHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" PHOTO_ID,ACTIVITY_ID,PHOTO_TITLE,MEMBER_ID,ADDTIME,STATE,AUDIT_TIME,AUDIT_USERNAME,AUDIT_STATE,PHOTO_IMG_FILE ");
            strSql.Append(" FROM COFCO_ACTIVITY_PHOTOS ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return SQLHelper.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "COFCO_ACTIVITY_PHOTOS";
            parameters[1].Value = "PHOTO_ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}

